package org.carl.component.job.service

import com.alibaba.fastjson.JSON
import com.xxl.job.core.biz.model.ReturnT
import com.xxl.job.core.executor.XxlJobExecutor
import jodd.http.Cookie
import jodd.net.HttpMethod
import org.carl.component.job.constant.PlatForm
import org.carl.component.job.util.HttpUtil
import org.carl.component.job.vo.XxlJobInfo
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Component


/**
 * email carl.chen@bk.ru
 * @Author: 陈青松
 * @Date: 2019/1/19 2:57
 */
@ConditionalOnBean(value = [XxlJobExecutor::class])
@Component
open class JobInfoService : JobService() {

    //登录
    private val login = "/login"
    //添加任务
    private val add = "/jobinfo/add"
    //更新任务
    private val update = "/jobinfo/update"
    //删除任务
    private val remove = "/jobinfo/remove"
    //暂停任务
    private val pause = "/jobinfo/pause"
    //恢复任务
    private val resume = "/jobinfo/resume"
    //触发任务,可以为未来的某个时候
    private val execute = "/jobinfo/execute"
    /**
     * 立即触发任务
     */
    private val trigger = "/jobinfo/trigger"
    //任务列表
    private val pageList = "/jobinfo/pageList"
    //根据组获取任务
    private val getJobsByGroup = "/joblog/getJobsByGroup"

    /**
     * 登录
     */
    fun login(): ResponseEntity<String> {
        var request = handleHttpEntity(this!!.xxlConfigVo!!)
        return restTemplate!!.postForEntity(getLoadUrl(login), request, String::class.java, arrayOfNulls<Any>(0))
    }

    /**
     * 添加任务
     */
    fun addJob(xxlJobInfo: XxlJobInfo): ResponseEntity<String> {
        var request = handleHttpEntity(xxlJobInfo)
        return restTemplate!!.postForEntity(getLoadUrl(add), request, String::class.java, arrayOfNulls<Any>(0))
    }

    /**
     * 更新任务
     */
    fun updateJob(xxlJobInfo: XxlJobInfo): ResponseEntity<String> {
        var request = handleHttpEntity(xxlJobInfo)
        return restTemplate!!.postForEntity(getLoadUrl(update), request, String::class.java, arrayOfNulls<Any>(0))
    }

    /**
     * 删除任务
     */
    fun removeJob(xxlJobInfoId: Int?): ResponseEntity<String> {
        var request = handleHttpEntity(xxlJobInfoId!!)
        return restTemplate!!.postForEntity(getLoadUrl(remove), request, String::class.java, arrayOfNulls<Any>(0))
    }

    /**
     * 暂停任务
     */
    fun pauseJob(xxlJobInfoId: Int): ResponseEntity<String> {
        var request = handleHttpEntity(xxlJobInfoId)
        return restTemplate!!.postForEntity(getLoadUrl(pause), request, String::class.java, arrayOfNulls<Any>(0))
    }

    /**
     * 任务列表
     */
    fun pageList(start: Int, length: Int, jobGroup: Int, jobDesc: String, executorHandler: String, filterTime: String): ResponseEntity<String> {
        //创建参数
        val paramMap = mapOf(
                "start" to start,
                "length" to length,
                "jobGroup" to jobGroup,
                "jobDesc" to jobDesc,
                "executorHandler" to executorHandler,
                "filterTime" to length
        )
        var request = handleHttpEntity(paramMap)
        return restTemplate!!.postForEntity(getLoadUrl(pageList), request, String::class.java, arrayOfNulls<Any>(0))
    }

    /**
     * 恢复任务
     */
    fun resumeJob(xxlJobInfoId: Int): ResponseEntity<String> {
        var request = handleHttpEntity(xxlJobInfoId)
        return restTemplate!!.postForEntity(getLoadUrl(resume), request, String::class.java, arrayOfNulls<Any>(0))
    }

    /**
     * 触发执行任务
     */
    fun trigger(xxlJobInfoId: Int, executorParam: String): ResponseEntity<String> {
        //创建参数
        val paramMap = mapOf("id" to xxlJobInfoId, "executorParam" to executorParam)
        var request = handleHttpEntity(paramMap)
        return restTemplate!!.postForEntity(getLoadUrl(trigger), request, String::class.java, arrayOfNulls<Any>(0))
    }
    /**
     * 触发执行任务,参数为任务id,触发类型,重试次数,执行参数
     */
    fun trigger(xxlJobInfoId: Int,cronExpression:String?,executorParam:String): ReturnT<*>{
        //创建参数
        val paramMap = mapOf(
                "id" to xxlJobInfoId,
                "cronExpression" to cronExpression,
                "executorParam" to executorParam
        )
//        var request = handleHttpEntity(paramMap)
        val hr = HttpUtil.send(HttpMethod.GET, getLoadUrl(execute), paramMap, null, Cookie(PlatForm.COOKIE))
        val body = HttpUtil.getUtf8(hr!!)
        body?.let{
            return JSON.parseObject(it,ReturnT::class.java)
        }
//        return restTemplate!!.postForEntity(getLoadUrl(execute), request, ReturnT::class.java, arrayOfNulls<Any>(0))
    }

    /**
     * 根据任务组获取任务列表
     */
    fun getJobsByGroup(jobGroup: Int): Any? {
        val paramMap = mapOf("jobGroup" to jobGroup)
        val hr = HttpUtil.send(HttpMethod.GET, getLoadUrl(getJobsByGroup), paramMap, null, Cookie(PlatForm.COOKIE))
        val body = HttpUtil.getUtf8(hr!!)
        body?.let{
            return getObject(JSON.parseObject(it,ReturnT::class.java),XxlJobInfo::class.java)
        }
        return null
    }

    /**
     * 根据class查找XxlJobInfo
     */
    fun getXxlJobInfoId(clazz:Class<*>): XxlJobInfo? {
        var xxlJobInfo:XxlJobInfo?=null
        val jobHandler:String= PlatForm.JOB_HANDLER_MAP!![clazz]!!
        jobHandler?.let{
            xxlJobInfo= PlatForm.JOB_LIST!!.find { x-> it == x.executorHandler }
        }
        return xxlJobInfo
    }
}
